package com.yueya.business.bed.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.yueya.business.senior.model.SeniorBase;
import com.yueya.common.util.UUIDGenerator;
/**
 * 
 * @author 41654
 *
 */
@SuppressWarnings("serial")
public class Bed extends Model<Bed> {

	
	public static Bed dao = new Bed();
	
	
	public boolean save(Bed dao){
		dao.set("bed_id", UUIDGenerator.getUUID());
		return dao.save();
	}
	
	public boolean update(Bed dao){
		return dao.update();
	}
	
	public boolean remove(Bed dao){
		dao.set("del_flag", '1');
		return dao.update();
	}
	
	
	//床铺
	public static  String no() {
			String number ="";
			String tag ="B";
			long count = 1 + Db
					.queryLong("SELECT COUNT(bed_id)  FROM jg_bed ");
			if (0 < count) {
				if (10 > count)
					number = tag + "000" + count;
				else if (100 > count)
					number = tag + "00" + count;
				else if (1000 > count)
					number = tag + "0" + count;
				else
					number = tag + count;
			}
			return number;
	}
	
	
	 public List<Bed> getList(String  floor_id) {
		 return Bed.dao.find("select * from jg_bed where room_id=? order by bed_no ",floor_id);
	 }
	 
	public List<Map<String, Object>>  getListAll(String rood_id) {
		    List<Map<String, Object>>  result = new ArrayList<Map<String, Object>>();
		    List<Bed>  list = getList(rood_id);
		    Iterator<Bed> it = list.iterator();
		    while(it.hasNext()) {
		    	Map<String, Object> map =new HashMap<String, Object>();
		    	Bed bed = it.next();
		    	map.put("id", bed.getStr("bed_id"));
		    	map.put("name", bed.getStr("bed_name"));
		    	SeniorBase senior  = SeniorBase.dao.findByBedid(bed.getStr("bed_id"));
		    	if(StrKit.notNull(senior)) {
		    		map.put("status", "1");
		    		map.put("senior", senior);
		    	}else {
		    		map.put("status", "0");
		    	}
		    	result.add(map);
		     }	
		 return result;
	}
	
	
	@SuppressWarnings("rawtypes")
	public List getBedStatistics(Map<String,String> map) {
		StringBuffer sql = new StringBuffer(" select reserv.*  from (");
		sql.append(" SELECT h.house_id, h.house_name,h.org_id,r.room_name,r.room_type, b.bed_name, ");
		sql.append(" ( select  EXISTS ( SELECT senior_id FROM jg_stay	WHERE bed_id = b.bed_id) ) as bed_status ");
		sql.append(" FROM jg_house h ");
		sql.append(" INNER JOIN jg_floor f ON f.house_id = h.house_id ");
		sql.append(" INNER JOIN jg_room r ON f.floor_id = r.floor_id  ");
		sql.append(" INNER JOIN jg_bed b ON b.room_id = r.room_id ) reserv where 1=1  ");
	    for (Map.Entry<String, String> entry : map.entrySet()) {
	          if(entry.getValue().contains("%")) {
	        	  sql.append(" and reserv." +entry.getKey()+ "like '"+entry.getValue()+"'  ");
	          }else if(StrKit.notBlank(entry.getValue())) {
	       	     sql.append(" and reserv." +entry.getKey()+ " ='"+entry.getValue()+"'  ");
	       	  }           
	      }  
	    List li =	Db.query(sql.toString());
		return li;
	}
}
